package com.xiaozhi.service.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaozhi.common.exception.BasicInfoException;
import com.xiaozhi.common.exception.BasicInfoStatusEnum;
import com.xiaozhi.dto.UserDTO;
import com.xiaozhi.dto.UserLoginDTO;
import com.xiaozhi.dao.UserTokenMapper;
import com.xiaozhi.request.UserLoginReqVO;
import com.xiaozhi.response.UserLoginRespVO;
import com.xiaozhi.service.WxUserService;
import com.xiaozhi.vo.UserTokenVO;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;

@Service
@AllArgsConstructor
//@NoArgsConstructor
@Slf4j
public class WxUserServerImpl implements WxUserService {
    //@Resource
  // private final WxMaService wxMaService;

    //@Resource
    @Autowired
    private    UserTokenMapper userTokenMapper;

/* 
    @Override
    public UserLoginRespVO userLogin(UserLoginReqVO userLoginReqVO) {

        // 请求微信 auth.code2Session 接口，换取用户唯一标识 openid
        String openid = "";
        String sessionKey = "";
        try {
            WxMaJscode2SessionResult wxMaJscode2SessionResult = wxMaService.getUserService()
                    .getSessionInfo(userLoginReqVO.getCode());
            openid = wxMaJscode2SessionResult.getOpenid();
            sessionKey = wxMaJscode2SessionResult.getSessionKey();
        } catch (WxErrorException e) {
            log.error("请求微信接口code2Session，获取用户唯一标识 openid异常:{}", e.fillInStackTrace());
            throw new BasicInfoException(BasicInfoStatusEnum.THIRD_SERVICE_UNAVAILABLE.getCode(), "请求微信服务失败");
        }

        UserLoginDTO userLoginDTO = new UserLoginDTO().setOpenid(openid);

        // 校验和解密用户信息
        if (StrUtil.isNotBlank(userLoginReqVO.getRawData())
                && StrUtil.isNotBlank(userLoginReqVO.getSignature())
                && StrUtil.isNotBlank(userLoginReqVO.getIv())
                && StrUtil.isNotBlank(userLoginReqVO.getEncryptedData())) {


            // 用户信息校验
            Assert.isTrue(
                    wxMaService.getUserService().checkUserInfo(sessionKey, userLoginReqVO.getRawData(),
                            userLoginReqVO.getSignature()),
                    () -> new BasicInfoException(BasicInfoStatusEnum.PARAM_ERROR.getCode(), "用户信息校验失败"));

            // 解密用户信息
            WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey,
                    userLoginReqVO.getEncryptedData(), userLoginReqVO.getIv());

            userLoginDTO.setAvatarUrl(userInfo.getAvatarUrl()).setNickName(userInfo.getNickName())
                    .setGender(StrUtil.isNotBlank(userInfo.getGender()) ? Integer.valueOf(userInfo.getGender()) : 0);
            String token = IdUtil.fastSimpleUUID() + RandomUtil.randomString(8);
            UserLoginRespVO userLoginRespVO = new UserLoginRespVO();
                    userLoginRespVO.setAvatarUrl(userInfo.getAvatarUrl()).setNickName(userInfo.getNickName())
                    .setGender(StrUtil.isNotBlank(userInfo.getGender()) ? Integer.valueOf(userInfo.getGender()) : 0).setToken(token);
            return userLoginRespVO;
        }
        return null;
    }
    @Override

    public UserLoginRespVO userInfo(UserLoginReqVO userLoginReqVO)
    {
        String openid = "";
        String sessionKey = "";
        try {
            WxMaJscode2SessionResult wxMaJscode2SessionResult = wxMaService.getUserService()
                    .getSessionInfo(userLoginReqVO.getCode());
            openid = wxMaJscode2SessionResult.getOpenid();
            sessionKey = wxMaJscode2SessionResult.getSessionKey();
        } catch (WxErrorException e) {
            log.error("请求微信接口code2Session，获取用户唯一标识 openid异常:{}", e.fillInStackTrace());
            throw new BasicInfoException(BasicInfoStatusEnum.THIRD_SERVICE_UNAVAILABLE.getCode(), "请求微信服务失败");
        }

        UserLoginDTO userLoginDTO = new UserLoginDTO().setOpenid(openid);

        // 校验和解密用户信息
        if (StrUtil.isNotBlank(userLoginReqVO.getRawData())
                && StrUtil.isNotBlank(userLoginReqVO.getSignature())
                && StrUtil.isNotBlank(userLoginReqVO.getIv())
                && StrUtil.isNotBlank(userLoginReqVO.getEncryptedData())) {


            // 用户信息校验
            Assert.isTrue(
                    wxMaService.getUserService().checkUserInfo(sessionKey, userLoginReqVO.getRawData(),
                            userLoginReqVO.getSignature()),
                    () -> new BasicInfoException(BasicInfoStatusEnum.PARAM_ERROR.getCode(), "用户信息校验失败"));

            // 解密用户信息
            WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey,
                    userLoginReqVO.getEncryptedData(), userLoginReqVO.getIv());

            userLoginDTO.setAvatarUrl(userInfo.getAvatarUrl()).setNickName(userInfo.getNickName())
                    .setGender(StrUtil.isNotBlank(userInfo.getGender()) ? Integer.valueOf(userInfo.getGender()) : 0);
            String token = IdUtil.fastSimpleUUID() + RandomUtil.randomString(8);
            UserLoginRespVO userLoginRespVO = new UserLoginRespVO();
                    userLoginRespVO.setAvatarUrl(userInfo.getAvatarUrl()).setNickName(userInfo.getNickName())
                    .setGender(StrUtil.isNotBlank(userInfo.getGender()) ? Integer.valueOf(userInfo.getGender()) : 0).setToken(token);
            return userLoginRespVO;
        }
        return null;
    }
    */
    @Override
    public UserTokenVO getUserFromToken(String token)
    {
        return userTokenMapper.getUserFromToken(token);
    }
    @Override
    public void addUserTokenVO(UserTokenVO vo)
    {
        userTokenMapper.addUserTokenVO(vo);
    }
    @Override
    public void update(UserTokenVO vo)
    {
        userTokenMapper.update(vo);
    }
}
